#include <iostream>
#include <fstream>


#include "HeightWidth.h"
#include "dateimanager.h"
#include "feld.h"
void Dateimanager::load(const std::string& path, Feld& f, Regelwerk *pr) {
  std::ifstream s;

  std::string date, descr, line;
  int height, width, gen;

  s.open(path.c_str());
 getline(s, date, ';' );
 getline(s, descr, ';' );
  s >> height;
  s >> width;
  s >> gen;

  HeightWidth hw;
  hw.setHeight(height);
  hw.setWidth(width);
  f.setHeightWidth(hw);
  f.setGen(gen);
	
	//Regelwerk* pr = new Regelwerk;
	
	for (int i = 0; i < 2; ++i) {
		s >> line;
		for (int j = 0; j < 9; ++j) {
			if (line[j] == '0') pr->setRule(i, j, false);
			else pr->setRule(i, j, true);
		}
	}
	//f.setRegelwerk(pr);

  for (int i = 0; i < height; ++i) {
    s >> line;
    for (int j = 0; j < width; ++j) {
			HeightWidth pos(i, j);
      if (line[j] == '0') f.setCell(pos, false);
      else f.setCell(pos, true);
    }
  }

  s.close();
}

void Dateimanager::save(const std::string& path, const std::string& descr, const std::string& date, const Feld& f, Regelwerk *pr) {
  std::ofstream s;

  s.open(path.c_str());
  s << date <<";"<< std::endl;
  s << descr <<";"<< std::endl;
  s << f.getHeight() << std::endl;
  s << f.getWidth() << std::endl;
  s << f.getGen() << std::endl;
	
	//Regelwerk* pr = f.getRegelwerk();
	
	for (int i = 0; i < 2; ++i) {
		for (int j = 0; j < 9; ++j) {
			s << pr->getRule(i, j);
		}
		s << std::endl;
	}

  for (int i = 0; i < f.getHeight(); ++i) {
    for (int j = 0; j < f.getWidth(); ++j) {
			HeightWidth pos(i, j);
      s << f.getCell(pos);
    }
    s << std::endl;
  }

  s.close();
}

std::string Dateimanager::getDescr(const std::string& path) {
    std::ifstream s;
    std::string date, result;
    
    s.open(path.c_str());
    getline(s, date, ';' );
    getline(s, result, ';' );
    
    s.close();
    return result;
}
